package com.jt.tenant.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.IntegralInfoAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetIntegralList;
import com.jt.tenant.dialog.MyTimePopup;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by AD on 2016/5/23.
 */
public class IntegralInfoActivity extends JTActivity implements OnClickListener {

    private ListView lv_integral;

    private TextView mTitleName,mDate,get_integral,use_integral,integral;


    private List<GetIntegralList.IntegralList> mList = new ArrayList<>();

    private IntegralInfoAdapter mAdapter;

    private GetIntegralList mGetIntegralList;

    private MyTimePopup myTimePopup;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");

    private Calendar calendar = Calendar.getInstance();

    private String type = "1";

    private String flag = "";

    private ImageView iv_left,iv_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_info);
        flag = getIntent().getStringExtra("flag");
        initView();
        GetData();
    }

    private void initView() {
        if(flag.equals("1")) {

            (mTitleName = (TextView) findViewById(R.id.tv_title_name)).setText("定向积分");

        }else if(flag.equals("2")){

            (mTitleName = (TextView) findViewById(R.id.tv_title_name)).setText("通用积分");

        }else {

            (mTitleName = (TextView) findViewById(R.id.tv_title_name)).setText("我的积分");

        }
        lv_integral = (ListView) findViewById(R.id.integral_list);
        (iv_left = (ImageView) findViewById(R.id.iv_left)).setOnClickListener(this);
        (iv_right = (ImageView) findViewById(R.id.iv_right)).setOnClickListener(this);
        (get_integral = (TextView)findViewById(R.id.get_integral)).setOnClickListener(this);//
        (use_integral = (TextView)findViewById(R.id.use_integral)).setOnClickListener(this);
        (integral = (TextView)findViewById(R.id.integral)).setText(Tools.F_num(JTApplication.jtPreferences.readUserIntegral(flag)));
        Log.e("IIIII====",JTApplication.jtPreferences.readUserIntegral(flag));
        mDate = (TextView) findViewById(R.id.tv_time);
        mDate.setText(simpleDateFormat.format(calendar.getTime()));
        mDate.setOnClickListener(this);
        mAdapter = new IntegralInfoAdapter(this,mList);
        lv_integral.setAdapter(mAdapter);

        myTimePopup = new MyTimePopup(this, null, new Date(), MyTimePopup.Type.YEAR_MONTH) {

            private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");

            @Override
            public void onSubmit(Date date) {

                mDate.setText(simpleDateFormat.format(date));

                mGetIntegralList.execute(IntegralInfoActivity.this);

                calendar.setTime(date);

                GetData();


            }

        };

        mGetIntegralList = new GetIntegralList(new AsyCallBack<GetIntegralList.Info>(){
            @Override
            public void onSuccess(String toast, int type, GetIntegralList.Info info) throws Exception {
                super.onSuccess(toast, type, info);

                if(info.msg.equals("000")) {

                    mList.clear();

                    mList.addAll(info.integralLists);
//                    int sum = 0;
//
//                    for(int i = 0;i<info.integralLists.size();i++){
//
//                        sum += Integer.valueOf(Tools.F_num(info.integralLists.get(i).score));
//
//                    }
//
//                    integral.setText(String.valueOf(sum));

                    mAdapter.notifyDataSetChanged();

                }else{

                    mList.clear();

                    mAdapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

                mList.clear();

                mAdapter.notifyDataSetChanged();

                if(!toast.equals("")){

                    UtilToast.show(IntegralInfoActivity.this, toast);

                }

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);



            }
        });


    }
    private void GetData(){

        mDate.setText(simpleDateFormat.format(calendar.getTime()));
        mGetIntegralList.date = mDate.getText().toString();
        mGetIntegralList.type = type;
        mGetIntegralList.flag = flag;
        mGetIntegralList.execute(this);
    }

    public void onClick(View v) {

        switch (v.getId()){
            case R.id.tv_time:
                myTimePopup.show(getWindow().getDecorView());
                break;
            case R.id.iv_left:
                calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);

                GetData();



                break;
            case R.id.iv_right:
                calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);

                Calendar acalendar = Calendar.getInstance();

                acalendar.set(Calendar.MONTH, acalendar.get(Calendar.MONTH));

                if(calendar.getTime().getTime() > acalendar.getTime().getTime()){

                    calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);

                }else{


                    GetData();


                }
                break;

            case R.id.get_integral:

                if(!type.equals("1")){

                    get_integral.setBackgroundColor(Color.parseColor("#eeeeee"));
                    use_integral.setBackgroundColor(Color.parseColor("#ffffff"));
                    type = "1";
                    GetData();

                }

                break;

            case R.id.use_integral:

                if(!type.equals("2")){

                    use_integral.setBackgroundColor(Color.parseColor("#eeeeee"));
                    get_integral.setBackgroundColor(Color.parseColor("#ffffff"));
                    type = "2";
                    GetData();

                }

                break;
        }
    }

}
